package validation.jexp;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class FeedBeanExample2 extends JPanel {
    JTextField nameField;
    JTextField siteField;
    JTextField feedField;
    JComboBox feedTypeComboBox;
    JCheckBox customCheckIntervalCheckBox;
    JTextField intervalField;
    Feed feed;

    public FeedBeanExample2() {
        DefaultFormBuilder formBuilder = new DefaultFormBuilder(new FormLayout(""));
        formBuilder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        formBuilder.appendColumn("right:pref");
        formBuilder.appendColumn("3dlu");
        formBuilder.appendColumn("fill:p:g");
        this.nameField = new JTextField();
        formBuilder.append("Name:", this.nameField);
        this.siteField = new JTextField();
        formBuilder.append("Site:", this.siteField);
        feedField = new JTextField();
        formBuilder.append("Feed:", this.feedField);
        this.feedTypeComboBox = new JComboBox(FeedType.TYPES.toArray());
        formBuilder.append("Feed Type:", this.feedTypeComboBox);
        this.customCheckIntervalCheckBox = new JCheckBox();
        formBuilder.append("Custom Check Interval:", this.customCheckIntervalCheckBox);
        this.intervalField = new JTextField();
        formBuilder.append("Interval:", this.intervalField);
        formBuilder.append(new JButton(new ShowFeedInformationAction()), 3);
        createFeed();
        initializeFormElements();
        add(formBuilder.getPanel());
    }

    private void createFeed() {
        this.feed = new Feed();
        this.feed.setName("ClientJava.ru");
        this.feed.setSiteUrl("http://www.clientjava.ru/blog");
        this.feed.setFeedUrl("http://www.clientjava.ru/blog/rss.xml");
        this.feed.setFeedType(FeedType.RSS_20);
        this.feed.setCustomCheckIntervalEnabled(true);
        this.feed.setCheckInterval(new Integer(4));
    }

    private void initializeFormElements() {
        this.nameField.setText(this.feed.getName());
        this.siteField.setText(this.feed.getSiteUrl());
        this.feedField.setText(this.feed.getFeedUrl());
        this.feedTypeComboBox.setSelectedItem(this.feed.getFeedType());
        this.customCheckIntervalCheckBox.setSelected(this.feed.isCustomCheckIntervalEnabled());
        if (this.feed.getCheckInterval() != null) {
            this.intervalField.setText(this.feed.getCheckInterval().toString());
        }
    }

    private void syncFormAndFeed() {
        this.feed.setName(this.nameField.getText());
        this.feed.setSiteUrl(this.siteField.getText());
        this.feed.setFeedUrl(this.feedField.getText());
        this.feed.setFeedType((FeedType) this.feedTypeComboBox.getSelectedItem());
        this.feed.setCustomCheckIntervalEnabled(this.customCheckIntervalCheckBox.isSelected());
        if (this.feed.isCustomCheckIntervalEnabled()) {
            this.feed.setCheckInterval(new Integer(this.intervalField.getText()));
        } else {
            this.feed.setCheckInterval(null);
        }
    }

    public Feed getFeed() {
        return feed;
    }

    private class ShowFeedInformationAction extends AbstractAction {
        private ShowFeedInformationAction() {
            super("Show Feed Information");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            syncFormAndFeed();
            StringBuffer messages = new StringBuffer();
            messages.append("<html>");
            messages.append("<b>Name:</b>");
            messages.append(getFeed().getName());
            messages.append("<br><b>Site:</b>");
            messages.append(getFeed().getSiteUrl());
            messages.append("<br><b>Feed:</b>");
            messages.append(getFeed().getFeedUrl());
            messages.append("<br><b>Feed Type:</b>");
            messages.append(getFeed().getFeedType());
            messages.append("<br><b>Custom Check:</b>");
            messages.append(getFeed().isCustomCheckIntervalEnabled());
            messages.append("<br><b>Check Interval:</b>");
            messages.append(getFeed().getCheckInterval());
            messages.append("</html>");
            JOptionPane.showMessageDialog(null, messages.toString());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JGoodiesValidationUtils.showFrame("Feed Bean Example2", new FeedBeanExample2());
        });
    }
}
